package com.five.taotao.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.five.taotao.Application.MyApplication;
import com.five.taotao.R;
import com.five.taotao.entity.Order;
import com.five.taotao.entity.Product;
import com.five.taotao.entity.ShoppingCart;
import com.five.taotao.order_details.OrderConfirmActivity;
import com.five.taotao.product_details.ProductDetailsActivity;
import com.five.taotao.util.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;


/**
 * Created by MQ on 2016/11/4.
 */
public class GoodsPayAdapter extends BaseAdapter {

    //定义两个变量，一个context一个集合
    private Context mContext;
    private List<Order.ListBean> mDatas;
    private Order.ListBean.Productid1Bean product;
    private Order.ListBean order;

    /**
     * 有参构造
     *
     * @param context 传入context
     * @param mDatas  传入数据
     */
    public GoodsPayAdapter(Context context, List<Order.ListBean> mDatas) {
        super();
        this.mContext = context;
        this.mDatas = mDatas;

    }


    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        order = mDatas.get(position);
        product = order.getProductid1();
        //布局加载器
        LayoutInflater inflater = LayoutInflater.from(mContext);//参数要有 上下文 Context
        ViewHolder holder;
        /**
         * ConvertView 回收视图，效率提高 200%
         *
         * 如果原先没有convertView，就加载布局
         */
        if (convertView == null) {
            //加载布局
            convertView = inflater.inflate(R.layout.pay_adapter_item, null, false);
            //缓存对象
            holder = new ViewHolder(convertView);//
            convertView.setTag(holder);//View 关联 Holder
        } else {

            holder = (ViewHolder) convertView.getTag();//若果有 加载 ViewHolder
        }

        holder.fukuan.setTag(position);//这段代码很重要
        holder.order_item.setTag(position);//这段代码很重要

        holder.name.setText(product.getName());
        holder.desc.setText(product.getIntroduce());
        holder.price.setText("￥" + product.getPrice());
        holder.vipprice.setText("￥" + product.getVipprice());
        holder.suliang.setText(order.getNum() + "件");
        holder.num.setText("共" + order.getNum() + "件商品，合计：");
        holder.mprice.setText("￥"+product.getVipprice() * order.getNum());
        Glide.with(mContext)
                .load(product.getUrl())
                .placeholder(R.drawable.store_default1)//占位图片
                .error(R.drawable.store_default1)
                .thumbnail(0.5f)
                .into(holder.img);
        /**
         * 取消订单
         */
        holder.quxiao.setTag(position);
        holder.quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Button btn = (Button) v;
                int po = (int) btn.getTag();
                mDatas.remove(po);
                notifyDataSetChanged();
                //同时更新后台
                OkHttpUtils
                        .post()
                        .url(Constants.OD_URL)
                        .addParams("orderids", order.getId() + "")
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                MyApplication.toast.toastShow("请求失败");
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                MyApplication.toast.toastShow("订单删除成功");
                            }
                        });
            }
        });

        holder.fukuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int tmp = (int) v.getTag();
                Order.ListBean ol= mDatas.get(tmp);
                Order.ListBean.Productid1Bean product = ol.getProductid1();

                Intent intent = new Intent(mContext, OrderConfirmActivity.class);
                ShoppingCart.ListBean shopcar = new ShoppingCart.ListBean(
                        product.getName(),product.getPrice(),product.getIntroduce(),product.getUrl(),(int)product.getVipprice(),
                        product.getSid1(),product.getPid1(),product.getStock(),product.getProductId());
                shopcar.setNum(order.getNum());
                intent.putExtra("shopcar", shopcar);
                mContext.startActivity(intent);
            }
        });

        holder.order_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int tmp = (int) v.getTag();
                Order.ListBean ol= mDatas.get(tmp);
                Order.ListBean.Productid1Bean order = ol.getProductid1();
                Product.ListBean product = new Product.ListBean(
                        order.getName(),
                        order.getPrice(),
                        order.getIntroduce(),
                        order.getUrl(),
                        order.getVipprice(),
                        (Product.ListBean.Sid1Bean)order.getSid1(),
                        (Product.ListBean.Pid1Bean)order.getPid1(),
                        order.getStock(),
                        order.getProductId()+""
                );
                Intent intent = new Intent(mContext, ProductDetailsActivity.class);
                intent.putExtra("product",product);
                mContext.startActivity(intent);
            }
        });
        return convertView;//返回View对象
    }


    static class ViewHolder {
        @BindView(R.id.img)
        ImageView img;
        @BindView(R.id.price)
        TextView price;
        @BindView(R.id.suliang)
        TextView suliang;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.desc)
        TextView desc;
        @BindView(R.id.vipprice)
        TextView vipprice;
        @BindView(R.id.num)
        TextView num;
        @BindView(R.id.quxiao)
        Button quxiao;
        @BindView(R.id.fukuan)
        Button fukuan;
        @BindView(R.id.mprice)
        TextView mprice;
        @BindView(R.id.order_item)
        RelativeLayout order_item;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}


